package Pages;

import com.beust.jcommander.internal.Lists;
import org.apache.commons.collections.list.TreeList;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import selenium.WebDriverWrapper;
import utils.Log4Test;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by Time to be on 08.07.2014.
 */
public class ComparisonPage extends BasePage {

    private static final String product = "//*[@id='head_banner_container']/div[3]/div/div/div[2]/table/thead/tr[1]/td[%d]/div/div[2]/a";
    private static final By keys = By.xpath("//td[@class='detail-title']");
    private static final String productDetails = "//td[@class='detail-title']/ancestor::tr/td[%d]";
    private static final By differenceButton = By.xpath("//*[@id='compare-menu']/ul/li[2]/a");

    private String[] myDifference = new String[50];
    private String[] rozetkaDifference = new String[50];

    private int rozetkaDifferenceSize = 0;
    private int myDifferenceSize = 0;

    public ComparisonPage(WebDriverWrapper driver) {
        super(driver);
    }

    @Override
    public boolean isOpened () {
        if (driver.getCurrentUrl().contains("comparison")) return true;
        else return false;
    }

    public String NameOfProductInCompare(int number) {
        Log4Test.inMethod("Name of Product to compare");
        return driver.findElement(By.xpath(String.format(product, (number+1) ))).getText();
    }

    public boolean ifListedDifferenseEqualsBrowsersDifferense() {
        int matches = 0;

        Log4Test.inMethod("If listed Differencies equals browser's deffenerencies");

        difference();
        browserDifference();

        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        if (rozetkaDifferenceSize == myDifferenceSize)
            return true;
        else
            return false;
    }

    private List<WebElement> getKeys() {
        return driver.findElements(keys);
    }

    private List<WebElement> getProductsDetails(int i) {
        return driver.findElements(By.xpath(String.format(productDetails, (i+1) )));
    }


    private void difference() {
        int j = 0;

        Log4Test.inMethod("difference");

        List<WebElement> keys = getKeys();
        List<WebElement> product_1 = getProductsDetails(1);
        List<WebElement> product_2 = getProductsDetails(2);

        for (int i = 0; i < keys.size(); i++) {
            if(!product_1.get(i).getText().equals(product_2.get(i).getText())) {
                myDifference[j] = keys.get(i).getText();
                System.out.println(myDifference[j]);
                j++;
                myDifferenceSize++;
            }
        }
        Log4Test.endOfMethod("difference");
    }

    private void browserDifference() {
        Log4Test.inMethod("browserDifference");

        driver.findElement(differenceButton).click();
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        List<WebElement> browserDifference = driver.findElements(keys);

        for(int i = 0; i < browserDifference.size(); i++) {
            if (!browserDifference.get(i).getText().isEmpty()) {
                rozetkaDifference[i] = browserDifference.get(i).getText();
                System.out.println(rozetkaDifference[i]);

                rozetkaDifferenceSize++;
            }
        }
        Log4Test.endOfMethod("browser's Difference");
    }

//end.
}
